package com.example.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.dao.RoleDao;
import com.example.demo.dao.RoleUserDao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.EtRole;
import com.example.demo.entity.EtRoleUser;
import com.example.demo.entity.EtUser;
import com.example.demo.service.RoleService;
import com.example.demo.system.YcException;
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class RoleServiceImpl implements RoleService{

	
	@Resource
	private RoleDao roleDao;
	@Resource
	private RoleUserDao roleUserDao;
	@Resource
	private UserDao userDao;
	@Override
	public List<EtRole> getAllRoleList() throws Exception {
		
		return roleDao.queryRole();
	}

	@Override
	public Map<String,Object> getRoleInfo(String roleId) throws Exception {
		EtRole role = roleDao.queryById(roleId);
		List<EtUser> users = roleUserDao.queryByRoleId(roleId);
		Map<String,Object> map=new HashMap<>();
		map.put("roleId", roleId);
		map.put("roleCode", role.getRoleCode());
		map.put("roleDescribe", role.getRoleDescribe());
		map.put("roleName", role.getRoleName());
		map.put("users", users);
		return map;
	}

	@Override
	public int addRole(EtRole role) throws Exception {
		String id = removeLine();
		role.setId(id);
		return roleDao.addRole(role);
	}

	@Override
	public void deleteRole(EtRole role) throws Exception {
		roleDao.deleteRole(role);
		roleUserDao.deleteById(role.getId());
	}

	@Override
	public int updateRole(EtRole role) throws Exception {
		EtRole etRole = roleDao.queryById(role.getId());
		if(etRole==null){
			throw new YcException("非法角色");
		}
		
		updateRole(role, etRole);
		
		return roleDao.updateRole(role);
	}

	@Override
	public EtUser addRoleUser(String roleId, String phoneNum) throws Exception {
		EtUser etUser = userDao.queryByMobile(phoneNum);
		if(etUser==null){
			throw new YcException("非法用户");
		}
		EtRoleUser roleUser = new EtRoleUser();
		roleUser.setId(removeLine());
		roleUser.setRoleId(roleId);
		roleUser.setUserId(etUser.getUserId());
		roleUserDao.addRoleUser(roleUser); 
		return etUser;
	}

	@Override
	public int deleteRoleUser(EtRoleUser roleUser) throws Exception {
		return roleUserDao.deleteRoleUser(roleUser);
	}
	
	private String removeLine(){
		String uuid = UUID.randomUUID().toString();
		return uuid.replace("-", "");
	}

	private void updateRole(EtRole des, EtRole src){
		if(des.getRoleCode()!=null && !StringUtils.equals("", des.getRoleCode())){
			src.setRoleCode(des.getRoleCode());
		}
		if(des.getRoleDescribe()!=null && !StringUtils.equals("", des.getRoleDescribe())){
			src.setRoleDescribe(des.getRoleDescribe());
		}
		
		if(des.getRoleName()!=null && !StringUtils.equals("", des.getRoleName())){
			src.setRoleName(des.getRoleName());
		}
		
		
		
	}
}
